#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"

// 创建链表头
list_t *create_list()
{
    list_t *head = (list_t *)malloc(sizeof(list_t));
    if (NULL == head)
    {
        perror("malloc failed\n");
        return NULL;
    }

    head->user = NULL;
    head->next = NULL;

    return head;
}

// 注册用户(尾插)
int insert_user(list_t *head, user_t *user)
{

    if (head == NULL || user == NULL)
    {
        perror("head is NULL or user is NULL\n");
        return -1;
    }

    // 找尾
    list_t *temp = head;
    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    list_t *newnode = (list_t *)malloc(sizeof(list_t));
    if (newnode == NULL)
    {
        perror("malloc failed\n");
        return -1;
    }

    newnode->user = (user_t *)malloc(sizeof(user_t));
    if (newnode->user == NULL)
    {
        perror("malloc failed\n");
        return -1;
    }

    *newnode->user = *user;
    newnode->next = temp->next;

    temp->next = newnode;

    return 0;
}

// 比较用户的信息
// 第一个为链表中的信息，第二个为页面过来的信息
typedef int (*cmpfun_t)(user_t *listUser, user_t *user);

int cmpUserName(user_t *listUser, user_t *user)
{
    return strcmp(listUser->name, user->name);
}
int cmpUserPassword(user_t *listUser, user_t *user)
{
    return strcmp(listUser->password, user->password);
}

int cmpUserAll(user_t *listUser, user_t *user)
{
    int a, b;
    // printf("----------------------------------------------\n");
    // printf("%s\t%s\n",user->name,user->password);
    // printf("%s\t%s\n",listUser->name,listUser->password);
    a = strcmp(listUser->name, user->name);
    // printf("a = %d\n",a);
    b = strcmp(listUser->password, user->password);
    // printf("b = %d\n",b);
    if (a == 0 && b == 0)
    {
        return 0;
    }
    return -1;
}

// 查询用户
user_t *find_user(list_t *head, user_t *user, cmpfun_t cmpfun)
{

    if (head == NULL || user == NULL || head->next == NULL)
    {
        return NULL;
    }

    // printf("%s\t%s\n",user->name,user->password);
    list_t *temp = head->next;

    while (temp != NULL)
    {

        if (cmpfun(temp->user, user) == 0)
        {
            // printf("name:%s\tpassword:%s\tflag:%d\n",temp->user->name,temp->user->password,temp->user->flag);
            return temp->user;
        }
        temp = temp->next;
    }

    return NULL;
}

// 查询链表长度（不包括头）
int length_list(list_t *head)
{
    if (head == NULL || head->next == NULL)
    {
        return 0;
    }

    list_t *temp = head->next;
    int i = 1;
    while (temp->next != NULL)
    {
        ++i;
        temp = temp->next;
    }

    return i;
}

// 删除最后一个节点，因为文件读取的会把'\n'当成最后一行
// 即链表会多出一个数据，重复有效数据的最后一行
int delete_tlist(list_t *head)
{

    if (head == NULL)
    {
        perror("error\n");
        return -1;
    }

    list_t *temp = head->next;
    list_t *before = head;
    while (temp->next != NULL)
    {
        before = temp;
        temp = temp->next;
    }

    before->next = temp->next;
    free(temp->user);
    free(temp);
    return 0;
}

// 将链表中的数据存储到文件中
void save_user_file(list_t *head)
{

    if (head == NULL || head->next == NULL)
    {
        return;
    }
    FILE *fp;
    list_t *temp = head->next;
    fp = fopen("userFile.txt", "w+"); // w+ 清空重写，r+ 不会清空，只会从头开始写，保留原文件中没有被覆盖的内容
    if (fp == NULL)
    {
        perror("打开文件失败\n");
        return;
    }

    fprintf(fp, "账号\t密码\t标记\n");
    while (temp != NULL)
    {
        fprintf(fp, "%s\t%s\t%d\n", temp->user->name, temp->user->password, temp->user->flag);
        temp = temp->next;
    }

    fclose(fp);
}

// 每次注册成功把该条写到文件中
void save_oneUser_file(list_t *head)
{

    if (head == NULL)
    {
        return;
    }

    FILE *fp;
    list_t *temp = head->next;

    // 把指针一到链表尾部
    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    fp = fopen("userFile.txt", "a");
    if (fp == NULL)
    {
        perror("打开文件失败\n");
        return;
    }

    fprintf(fp, "%s\t%s\t%d\n", temp->user->name, temp->user->password, temp->user->flag);

    fclose(fp);
}

// 读取文件中的数据到链表中
void read_user_file(list_t *head)
{

    if (head == NULL)
    {
        return;
    }

    FILE *fp;
    char buf[1024], name[16], password[16];

    fp = fopen("userFile.txt", "r");
    if (fp == NULL)
    {
        perror("打开文件失败\n");
        return;
    }

    // 不读出第一行
    fgets(buf, sizeof(buf), fp);//fgets()函数从指定的文件中读取数据，每次读取一行，直到文件结束或遇到文件结束符EOF为止。

    user_t user;
    while (!feof(fp))
    {

        fscanf(fp, "%s%s%d", name, password, &user.flag);
        strcpy(user.name, name);
        strcpy(user.password, password);
        insert_user(head, &user);
    }

    fclose(fp);
}